package com.fengxu.gamescript.service

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.app.Service
import android.content.Intent
import android.graphics.Path
import android.os.Build
import android.os.IBinder
import android.view.accessibility.AccessibilityEvent
import androidx.annotation.RequiresApi
import org.jetbrains.anko.toast

/**
 * 自动化无障碍无法
 */
class AutoAccessibilityService : AccessibilityService() {

    companion object{

        private var isRuning: Boolean = false

        var accessibilityService: AutoAccessibilityService? = null

        /**
         * 判断无障碍是否在运行
         */
        fun serviceIsRunning(): Boolean{
            return isRuning
        }
    }

    override fun onServiceConnected() {
        super.onServiceConnected()
        isRuning = true
        accessibilityService = this
        startForeground(0,null)
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) = Unit

    override fun onInterrupt() = Unit

    override fun onDestroy() {
        isRuning = false
        accessibilityService = null
        stopForeground(true)
        super.onDestroy()
    }

    /**
     * 点击指定位置
     * @param x X坐标
     * @param y Y坐标
     * @param time 点击时长
     */
    fun autoClick(x: Double, y: Double, time: Int){
        var path = Path()
        path.moveTo(x.toFloat(), y.toFloat())
        var strokeDescription = GestureDescription.StrokeDescription(path, 0, time.toLong())
        dispatchGesture(GestureDescription.Builder().addStroke(strokeDescription).build(),null,null)
    }

    /**
     * 从(x1,y1)处 移动到 (x2,y2)处
     * @param time 用时ms
     */
    fun move(x1: Float, y1:Float, x2:Float, y2:Float, time:Int){
        var path = Path()
        path.moveTo(x1,  y1)
        path.lineTo(x2, y2)
        var strokeDescription = GestureDescription.StrokeDescription(path, 0, time.toLong())
        dispatchGesture(GestureDescription.Builder().addStroke(strokeDescription).build(),null,null)
    }

}